#include "serialport.h"
#include "ui_serialport.h"

#include <QDebug>

SerialPort::SerialPort(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SerialPort)
{
    ui->setupUi(this);

    m_serial = new Serial;
    connect(m_serial, SIGNAL(signalReadyRead(quint8)),
            this, SLOT(slotReadyRead(quint8)));
    ui->cb_baud_rate->setCurrentIndex(7);//默认115200
    ui->pb_open_com->setText(tr("打开串口"));
    ui->pb_send->setEnabled(false);
}

SerialPort::~SerialPort()
{
    delete ui;
}

void SerialPort::slotReadyRead(quint8 c)
{
    ui->te_recv_text->append(QString(c));
}

void SerialPort::on_pb_open_com_clicked()
{
    if(ui->pb_open_com->text()==tr("打开串口"))
    {
        m_serial->beginSerial();
        ui->pb_open_com->setText(tr("关闭串口"));
        ui->pb_send->setEnabled(true);
    }else
    {
        m_serial->stopSerial();
        ui->cb_com_port->setEnabled(true);
        ui->cb_baud_rate->setEnabled(true);
        ui->cb_crc_test->setEnabled(true);
        ui->cb_data_bit->setEnabled(true);
        ui->lb_statu_flag->setStyleSheet("background-color:rgb(130,130,130)");
        ui->pb_open_com->setText(tr("打开串口"));
        ui->pb_send->setEnabled(false);
    }
}

void SerialPort::on_pb_send_clicked()
{
    m_serial->sendString(ui->le_send_text->text());
}

void SerialPort::on_pb_clear_clicked()
{
    ui->te_recv_text->clear();
}
